<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\service\GoodsService;
use app\service\GoodsCommentsService;
use Think\Db;
use app\service\SearchService;
/**
 * 商品
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Goods extends Common
{
    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
    }

    /**
     * 获取商品详情
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-12
     * @desc    description
     */
    public function Detail()
    {
        // 参数
        if(empty($this->data_post['goods_id']))
        {
            return DataReturn('参数有误', -1);
        }

        // 商品详情方式
        $is_use_mobile_detail = intval(MyC('common_app_is_use_mobile_detail'));

        // 获取商品
        $goods_id = intval($this->data_post['goods_id']);
        $params = [
            'where' => [
                'g.id' => $goods_id,
                'is_delete_time' => 0,
                'is_shelves' => 1,
                'g.status' => 1,
               
            ],
            'is_photo' => true,
            'is_spec' => true,
            'is_content_app' => ($is_use_mobile_detail == 1),
        ];
        
        $ret = GoodsService::GoodsList($params);
        
       
        if(empty($ret['data'][0]) || $ret['data'][0]['is_delete_time'] != 0)
        {
            return DataReturn('商品不存在或已删除', -1);
        }

        // 商品详情处理
        if($is_use_mobile_detail == 1)
        {
            unset($ret['data'][0]['content_web']);
        } else {
            // 标签处理，兼容小程序rich-text
            $search = [
                '<img ',
                '<section',
                '/section>',
                '<p>',
                '<div>',
                '<table',
                '<tr',
                '<td',
            ];
            $replace = [
                '<img style="max-width:100%;margin:0;padding:0;display:block;" ',
                '<div',
                '/div>',
                '<p style="margin:0;">',
                '<div style="margin:0;">',
                '<table style="width:100%;margin:0px;border-collapse:collapse;border-color:#ddd;border-style:solid;border-width:0 1px 1px 0;"',
                '<tr style="border-top:1px solid #ddd;"',
                '<td style="margin:0;padding:5px;border-left:1px solid #ddd;"',
            ];
            $ret['data'][0]['content_web'] = str_replace($search, $replace, $ret['data'][0]['content_web']);
        }

        // 当前登录用户是否已收藏
        $ret_favor = GoodsService::IsUserGoodsFavor(['goods_id'=>$goods_id, 'user'=>$this->user]);
        $ret['data'][0]['is_favor'] = ($ret_favor['code'] == 0) ? $ret_favor['data'] : 0;

        // 商品评价总数
        $ret['data'][0]['comments_count'] = GoodsCommentsService::GoodsCommentsTotal(['goods_id'=>$goods_id, 'is_show'=>1]);

        // 商品访问统计
        GoodsService::GoodsAccessCountInc(['goods_id'=>$goods_id]);

        // 用户商品浏览
        GoodsService::GoodsBrowseSave(['goods_id'=>$goods_id, 'user'=>$this->user]);

        // 商品所属分类名称
        $category = GoodsService::GoodsCategoryNames($goods_id);
        $ret['data'][0]['category_names'] = $category['data'];

        // // 数据返回
        // $result = [
        //     'goods'                             => $ret['data'][0],
        //     'common_order_is_booking'           => (int) MyC('common_order_is_booking'),
        //     'common_app_is_use_mobile_detail'   => $is_use_mobile_detail,
        //     'common_app_is_online_service'      => (int) MyC('common_app_is_online_service'),
        //     'common_app_is_limitedtimediscount' => (int) MyC('common_app_is_limitedtimediscount'),
        //     'common_app_is_good_thing'          => (int) MyC('common_app_is_good_thing'),
        //     'common_app_is_poster_share'        => (int) MyC('common_app_is_poster_share'),
        // ];

        // // 秒杀
        // if($result['common_app_is_limitedtimediscount'] == 1)
        // {
        //     $ret = CallPluginsServiceMethod('limitedtimediscount', 'Service', 'GoodsDetailCountdown', $goods_id);
        //     if($ret['code'] == 0)
        //     {
        //         $result['plugins_limitedtimediscount_data'] = $ret['data'];
        //     }
        // }
        return DataReturn('success', 0, $ret['data'][0]);
    }
    
    /*
     *根据分类id搜索产品
     */
    public  function GoodsCategoryList()
    {   
        $params =input();
       
        return SearchService::GoodsList($params);
    }

    /**
     * 用户商品收藏
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-17
     * @desc    description
     */
    public function Favor()
    {
        // 登录校验
        $this->IsLogin();

        // 开始操作
        $params = $this->data_post;
        $params['user'] = $this->user;
        return GoodsService::GoodsFavor($params);
    }

    /**
     * 商品规格类型
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function SpecType()
    {
        // 开始处理
        $params = $this->data_post;
        $ret = GoodsService::GoodsSpecType($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_type'];
        }
        return $ret;
    }

    /**
     * 商品规格信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function SpecDetail()
    {
        // 开始处理
        $params = $this->data_post;
        $ret = GoodsService::GoodsSpecDetail($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_base'];
        }
        return $ret;
    }

    /**
     * 商品分类
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function Category()
    {
        // 开始处理
        $params = $this->data_post;
        $data = GoodsService::GoodsCategory($params);
        return DataReturn('success', 0, $data);
    }

    /**
     * 商品评分
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-07-11
     * @desc    description
     * @return  [type]          [description]
     */
    public function GoodsScore()
    {
        if(empty($this->data_post['goods_id']))
        {
            return DataReturn('参数有误', -1);
        }

        // 获取商品评分
        return GoodsCommentsService::GoodsCommentsScore($this->data_post['goods_id']);
    }

    /**
     * 商品评论
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2019-05-13T21:47:41+0800
     */
    public function Comment()
    {
        // 参数
        $params = $this->data_post;

        // 分页
        $number = 10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

        // 条件
        $where = [
            'goods_id'      => $params['goods_id'],
            'is_show'       => 1,
        ];

        // 获取总数
        $total = GoodsCommentsService::GoodsCommentsTotal($where);
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);

        // 获取列表
        $data_params = array(
            'm'         => $start,
            'n'         => $number,
            'where'     => $where,
            'is_public' => 1,
        );
        $data = GoodsCommentsService::GoodsCommentsList($data_params);
        
        // 返回数据
        $result = [
            'number'            => $number,
            'total'             => $total,
            'page_total'        => $page_total,
            'data'              => $data['data'],
        ];
        return DataReturn('success', 0, $result);
    }

    /**
     * 商品海报
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2019-08-17T21:10:41+0800
     */
    public function Poster()
    {
        // 是否开启海报功能
        if(MyC('common_app_is_poster_share') == 1)
        {
            return CallPluginsServiceMethod('distribution', 'PosterGoodsService', 'GoodsCreateMiniWechat', $this->data_post);
        }
        return DataReturn('海报功能未启用', -100);
    }


    //最新上架-每日推荐-真品孤本
    public function goodsRecommend(){
        
      

        // $where['is_delete_time']=0; 
        // $where['is_shelves']=1; 
        // $where['status']=1;
        // $where['inventory']=['neq',0];
        
        $where="is_delete_time=0 and is_shelves=1 and status=1 and inventory!=0 ";

        if($this->data_post['type']=="new_upper"){
        	$where.=" and new_upper=1";
           // $where['new_upper']=1;
        }elseif($this->data_post['type']=="recommended_daily"){
        		$where.="and recommended_daily=1";
            //$where['recommended_daily']=1;
        }elseif($this->data_post['type']=="rare_treasures"){
        		$where.=" and rare_treasures=1";
             //$where['rare_treasures']=1;
        }elseif($this->data_post['type']=="is_home_recommended"){
        		$where.=" and is_home_recommended=1";
            // $where['is_home_recommended']=1;
        }


           
        $field ="id,title,author,inventory,quality,original_price,price,images,is_delete_time";


         
        // 分页
        $number = isset($this->data_post['number'])? intval($this->data_post['number']):10;
        $page = max(1, isset($this->data_post['page']) ? intval($this->data_post['page']) : 1);

      
        // 获取总数
        $total = Db::name('Goods')->where($where)->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);

        // 获取列表
         
        $data = Db::name('Goods')->field($field)->where($where)->limit($start, $number)->select();
        
        foreach ($data as $k => $v){
        	if(empty($v['author'])){
        		$data[$k]['author_name']= "未知";
        	}else{
        		$author_name = Db::name("author")->where(['id'=>$v['author']])->value('name');
	        	if(empty($author_name)){
	        		$data[$k]['author_name']= "未知";
	        	}
        	}
        	
        	
        }
      
            // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
          
       
        return DataReturn('success', 0, $result);
    }


    /*
     *更多品相
     */
    public function Moreproducts(){
        if(empty($this->data_post['isbn'])){
            return DataReturn('参数有误', -1);
        }



        $data = Db::name('Goods')->field("id,quality,phase,price")->where("isbn=".$this->data_post['isbn']." and inventory!=0 and is_shelves=1 and status=1")->limit(5)->select();
      
             
        return DataReturn('success', 0, $data);
        
    }

    /*
     *相关推荐
     */
    public function Relatedrecommendations(){
        if(empty($this->data_post['goods_id']))
        {
            return DataReturn('参数有误', -1);
        }

        //获取产品分类id
        $category_id=Db::name("goods_category_join")->where(['goods_id'=>$this->data_post['goods_id']])->value("category_id");
    
        // $data = Db::table('s_goods')
        // ->alias('sg')
        // ->join('s_goods_category_join sgcj','sg.id=sgcj.goods_id','LEFT')      
        // ->field("sg.id,sg.title,sg.author,sg.quality,sg.original_price,sg.price,sg.images,sg.is_delete_time")     
        // ->where('sgcj.category_id='.$category_id." and (sg.is_home_recommended=1 or sg.recommended_daily=1)")
        // ->limit(10)
        // ->select();
        
        $data=SearchService::GoodsList(['category_id'=>$category_id,'inventory'=>['neq',0]]);

        return DataReturn('success', 0, $data);
    }


    /*
     *热销榜
     */
    public function Hotlist(){
        // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('order_detail')->where("isbn!=''")->group('isbn')->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);

 

        $data=Db::name("order_detail")->where("isbn!=''")->field("count(id) num,goods_id,title,isbn")->limit($start,$number)->group('isbn')->order("num desc")->select();

        //是否有货
        foreach ($data as $key => $val) {

            $goods=Db::name("goods")->where("inventory>0  and isbn='".$val['isbn']."' and is_delete_time=0 and is_shelves=1 and status=1")->find();

          
            if(!empty($goods)){
            	
            	 
		        	if(empty($goods['author'])){
		        		$author_name= "未知";
		        	}else{
		        		$author_name = Db::name("author")->where(['id'=>$v['author']])->value('name');
			        	if(empty($author_name)){
			        		$author_name= "未知";
			        	}
		        	}
		        	
		        	
		        
                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                  $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];
            }else{

                $goods=Db::name("goods")->find($val['goods_id']);

                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                 $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];

            }
            
        }

          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
              
    }

    /*
     *收藏榜
     */
    public function Collectionlist(){
        // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('goods_favor')->where("isbn!=''")->group('isbn')->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("goods_favor")->where("isbn!=''")->field("count(id) num,goods_id,user_id,isbn")->limit($start,$number)->group('isbn')->order("num desc")->select();

        //是否有货
        foreach ($data as $key => $val) {

            $goods=Db::name("goods")->where("inventory>0  and isbn='".$val['isbn']."' and is_delete_time=0  and is_shelves=1 and status=1")->find();

          
            
            if(!empty($goods)){
            	
            	 
		        	if(empty($goods['author'])){
		        		$author_name= "未知";
		        	}else{
		        		$author_name = Db::name("author")->where(['id'=>$v['author']])->value('name');
			        	if(empty($author_name)){
			        		$author_name= "未知";
			        	}
		        	}
		        	
		        	
		        
                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                  $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];
            }else{

                $goods=Db::name("goods")->find($val['goods_id']);

                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                 $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];

            }
            
        }


          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
    }

    /*
     *热搜榜
     */
    public function Hotsearchlist(){
        // 参数
        $params = $this->data_request; 

     
        // 分页
        $number = isset($params['number'])? intval($params['number']):10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

          // 获取总数
        $total = Db::name('goods_search')->where("isbn!=''")->group('isbn')->count();
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);


        $data=Db::name("goods_search")->where("isbn!=''")->field("count(id) num,goods_id,isbn")->limit($start,$number)->group('isbn')->order("num desc")->select();



        //是否有货
        foreach ($data as $key => $val) {

            $goods=Db::name("goods")->where("inventory>0  and isbn='".$val['isbn']."' and is_delete_time=0  and is_shelves=1 and status=1")->find();

          
             
            if(!empty($goods)){
            	
            	 
		        	if(empty($goods['author'])){
		        		$author_name= "未知";
		        	}else{
		        		$author_name = Db::name("author")->where(['id'=>$v['author']])->value('name');
			        	if(empty($author_name)){
			        		$author_name= "未知";
			        	}
		        	}
		        	
		        	
		        
                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                  $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];
            }else{

                $goods=Db::name("goods")->find($val['goods_id']);

                $data[$key]['goods_id']=$goods['id'];
                $data[$key]['title']=$goods['title'];
                $data[$key]['author']=$goods['author'];
                $data[$key]['author_name']=$author_name;
                $data[$key]['quality']=$goods['quality'];
                $data[$key]['original_price']=$goods['original_price'];
                $data[$key]['price']=$goods['price'];
                $data[$key]['images']=$goods['images'];

            }
            
        }


          // 返回数据
        $result = [
            'total'         =>  $total,
            'page_total'    =>  $page_total,
            'data'          =>  $data,
        ];
        return DataReturn('success', 0, $result);
    }

}


?>